#include "vgprocess.h"
#include "GraphicsManager.h"
#include "FontManager.h"
#include "Base.h"
#include "core.h"
#include "Frustum.h"
#include "Camera.h"
#include "ThPSCamera.h"
#include "FPSCamera.h"
#include "InputManager.h"
#include "VertexTypes.h"

//CVGProcess::CVGProcess()
//{
//
//}

CVGProcess::~CVGProcess()
{
	int k = 0;
}

void CVGProcess::Init()
{
	
	m_pObject= new CObject3D(Vect3f(0.0f,1.7f,0.0f)	,0.0f	,0.0f		,0.0f);
	m_pObject2=new CObject3D(Vect3f(0.0f,5.0f,0.0f)	,0.0f	,-FLOAT_PI_VALUE/4	,0.0f);

	Vect2i res=CORE->GetGraphicsManager()->GetScreenRes();
	float ap=(float)(res.x/res.y);
	m_pFPS=new CFPSCamera(0.0f,50.0f,FLOAT_PI_VALUE/2,ap,m_pObject);
	m_pTPS=new CThPSCamera(0.0f,50.0f,FLOAT_PI_VALUE/2,ap,m_pObject,5.0f);
	m_pCamera= m_pTPS;
	m_pVigil=new CFPSCamera(0.0f,50.0f,FLOAT_PI_VALUE/2,ap,m_pObject2);
}

void CVGProcess::Deinit()
{
	int k = 0;
}

void CVGProcess::Update(float dt, float fps)
{
	CGraphicsManager*	gm=CORE->GetGraphicsManager();
	CInputManager*		im=CORE->GetInputManager();
	CThPSCamera*		tpc;

	tpc=(CThPSCamera*)m_pCamera;
	
	//modifico angulo segun movimiento del mouse
	Vect3i dm=im->GetMouseDelta();
	Vect3f l_pos=m_pObject->GetPosition();
	
	float l_yaw=m_pObject->GetYaw()-(dm.x*0.01f);
	float l_pitch=m_pObject->GetPitch()+(dm.y*0.01f);

	Vect3f l_dir=Vect3f(cos(l_yaw),0.0f,sin(l_yaw));

	if(im->IsDown(IDV_MOUSE,0))
	{
		m_pObject->SetYaw(l_yaw);
		m_pObject->SetPitch(l_pitch);
	}

	
	//zoom

	tpc->AddZoom(-dm.z*0.01f);

	//toggle cam
	
	if(im->IsUpDown(IDV_KEYBOARD,KEY_T)) m_CamToggle=!m_CamToggle;
	if(m_CamToggle) 
	{
		m_pFPS->SetObject3D(m_pObject);
		m_pTPS->SetObject3D(m_pObject);
	}
	else 
	{
		m_pFPS->SetObject3D(m_pObject2);	
		m_pTPS->SetObject3D(m_pObject2);	
	}
		

	//-------------------------------------------

	//toggle cam view

	if(im->IsUpDown(IDV_KEYBOARD, KEY_V)) m_ViewToggle=!m_ViewToggle;
	if(m_ViewToggle) m_pCamera=m_pTPS;
	else m_pCamera= m_pFPS;

	//------------------------------------------
	

	//modifico velocidad segun la tecla shift
	if(im->IsDown(IDV_KEYBOARD,KEY_LSHIFT)) m_speed=4.0f;
	else m_speed=2.0f;
	//-----------------------------------------

	//movimiento del personaje
		//avance
	if(im->IsDown(IDV_KEYBOARD,KEY_W))		m_pObject->SetPosition(l_pos+(l_dir*m_speed*dt));
	if(im->IsDown(IDV_KEYBOARD,KEY_S))		m_pObject->SetPosition(l_pos-(l_dir*m_speed*dt));
		//strafe
	l_dir=l_dir^Vect3f(0.0,1.0,0.0);
	
	if(im->IsDown(IDV_KEYBOARD,KEY_D))		m_pObject->SetPosition(l_pos-(l_dir*m_speed*dt));
	if(im->IsDown(IDV_KEYBOARD,KEY_A))		m_pObject->SetPosition(l_pos+(l_dir*m_speed*dt));
	//-------------------------------------------------------------

	//angulo para el peque;o sistema solar
	if(m_angle>2*FLOAT_PI_VALUE) m_angle=0;
	else m_angle+=dt;
	//----------------------------------------

	//yaw obj2
	static int l_state=0;

	switch(l_state)
	{
	case 0:
		m_obj2Yaw+=dt*0.5f;
		if(m_obj2Yaw>FLOAT_PI_VALUE/2) l_state=1;
		break;

	case 1:
		m_obj2Yaw-=dt*0.5f;
		if (m_obj2Yaw<0) l_state=0;
		break;
	default:

		break;
	}
	
	m_pObject2->SetYaw(m_obj2Yaw);

	//-------------------------------

	//check sphere onshight

	m_testSphereOnSight=gm->SphereVisible(m_testSpherePos,m_testSphereRadius);

	//-----------------------

}

void CVGProcess::RenderScene()
{
	Mat44f l_matrix,r,t,t2,r2;
	

	r.SetIdentity();
	r.RotByAngleY(m_angle);
	
	r2.SetIdentity();
	r2.RotByAngleY(10*m_angle);

	t.SetIdentity();
	t.Translate(Vect3f(0.0f,0.0f,3.0f));

	t2.SetIdentity();
	t2.Translate(Vect3f(0.0f,0.0f,0.8f));

	//sol, ejes y grid
	l_matrix.SetIdentity();
	CORE->GetGraphicsManager()->SetTransform(l_matrix);

	CORE->GetGraphicsManager()->DrawAxis(5.0);
	CORE->GetGraphicsManager()->DrawGrid(20.0,20,colWHITE);
	CORE->GetGraphicsManager()->DrawSphere(1,colYELLOW,20,20);

	//Tierra
	l_matrix.SetIdentity();
	l_matrix=(r*t*r);
	CORE->GetGraphicsManager()->SetTransform(l_matrix);
	CORE->GetGraphicsManager()->DrawSphere(0.5,colGREEN,20,20);
	//gm->DrawCube(1.0,colYELLOW);
	
	//luna
	l_matrix.SetIdentity();
	l_matrix=(r*t*r2*t2);
	
	CORE->GetGraphicsManager()->SetTransform(l_matrix);
	CORE->GetGraphicsManager()->DrawSphere(0.2f,colWHITE,20,20);


	//cubo

	l_matrix.SetIdentity();
	
	l_matrix.RotByAngleY(-m_pObject->GetYaw());
	l_matrix.Translate(m_pObject->GetPosition());
	
	CORE->GetGraphicsManager()->SetTransform(l_matrix);
	CORE->GetGraphicsManager()->DrawCube(1.0f,colYELLOW);

	//camara

	l_matrix.SetIdentity();
	l_matrix.RotByAngleY(-m_pObject2->GetYaw());
	l_matrix.Translate(m_pObject2->GetPosition());
	
	CORE->GetGraphicsManager()->SetTransform(l_matrix);
	CORE->GetGraphicsManager()->DrawCube(1.0f,colYELLOW);

	//draw frustum

	CORE->GetGraphicsManager()->DrawCamera(m_pVigil);

	//sphere
	l_matrix.SetIdentity();
	l_matrix.Translate(m_testSpherePos);
	CORE->GetGraphicsManager()->SetTransform(l_matrix);
	CORE->GetGraphicsManager()->DrawSphere(m_testSphereRadius,colWHITE,20,20);
	

	


}

void CVGProcess::RenderScene2D()
{
	CORE->GetFontManager()->DrawDefaultText(100,100,colWHITE,"Sup World!?");
	CORE->GetFontManager()->DrawLiteral(100,115,"HiWorld");
	int id_font = CORE->GetFontManager()->GetTTF_Id("arial");
	CORE->GetFontManager()->DrawText(100,130,colWHITE,id_font,"Sup World!? en arial");
	
}

uint32		CVGProcess::RenderDebugInfo		()
{
	uint32 l_yaccum;
	CFontManager* fm=CORE->GetFontManager();



	l_yaccum=CProcess::RenderDebugInfo();
	l_yaccum+=fm->DrawDefaultText(10,l_yaccum,colWHITE,"Yaw = %.2f",m_pObject->GetYaw());
	l_yaccum+=fm->DrawDefaultText(10,l_yaccum,colWHITE,"Pos_x = %.2f",m_pObject->GetPosition().x);
	l_yaccum+=fm->DrawDefaultText(10,l_yaccum,colWHITE,"Pos_z = %.2f",m_pObject->GetPosition().z);
	l_yaccum+=fm->DrawDefaultText(10,l_yaccum,colWHITE,"Yaw obj2 = %.2f",m_pObject2->GetYaw());
	if(m_testSphereOnSight)	l_yaccum+=fm->DrawDefaultText(10,l_yaccum,colWHITE,"Sphere OnSight");
	else	l_yaccum+=fm->DrawDefaultText(10,l_yaccum,colWHITE,"Sphere OffSight");
	

	return l_yaccum;

}


//TCOLORED_VERTEX